<?php
/**
 * Created by PhpStorm.
 * User: 胥九庆
 * Date: 2023/01/04
 * Time: 17:26
 */
declare (strict_types=1);

namespace app\common\model;

class CourseComment extends Base
{
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile,photo');
    }

    public function course()
    {
        return $this->hasOne(Course::class, 'id', 'course_id')->field('id,title');
    }

    public static function add($param, $uid)
    {
        parent::startTrans();
        try {
            $param['uid'] = $uid;
            parent::create($param);
            Course::where('id', $param['course_id'])->inc('total_comment')->update();

            parent::commit();
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '评论成功'];
    }
}